package cn.gzjp.modules.it.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.gzjp.common.persistence.Page;
import cn.gzjp.common.service.CrudService;
import cn.gzjp.modules.it.entity.Car;
import cn.gzjp.modules.it.entity.MemberCar;
import cn.gzjp.modules.it.dao.CarDao;

/**
 * 车辆Service
 * @author zengcp
 * @version 2018-01-26
 */
@Service
@Transactional(readOnly = true)
public class CarService extends CrudService<CarDao, Car> {
	
	@Autowired
	private MemberCarService memberCarService;

	public Car get(String id) {
		return super.get(id);
	}
	
	public List<Car> findList(Car car) {
		return super.findList(car);
	}
	
	public Page<Car> findPage(Page<Car> page, Car car) {
		return super.findPage(page, car);
	}
	@Transactional(readOnly = false)
	public void save(Car car) {
		Car m = get(car.getId());
		if(m!=null){
			super.save(car);
		}else{
			super.insert(car, car.getId());
		}
	}
	
	@Transactional(readOnly = false)
	public void save(Car car,String openid) {
		this.save(car);
		MemberCar mc = new MemberCar();
		mc.setCarId(car.getId());
		mc.setMemberId(openid);
		MemberCar memberCar = memberCarService.findByMemberIdAndCarId(mc);
		if(memberCar==null){
			mc.setSort(0);
			memberCarService.insert(mc);
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(Car car) {
		super.deleteByLogic(car);
	}
	
}